package com.gxa.logistics.service.impl;

import com.github.pagehelper.PageHelper;
import com.gxa.logistics.common.base.PageWrapperDTO;
import com.gxa.logistics.common.base.RespResult;
import com.gxa.logistics.common.util.PageResultUtil;
import com.gxa.logistics.domain.dto.RoleMenuSaveDTO;
import com.gxa.logistics.domain.dto.RoleReqListDTO;
import com.gxa.logistics.domain.dto.RoleSaveDTO;
import com.gxa.logistics.domain.dto.RoleUpdateDTO;
import com.gxa.logistics.domain.entity.Menu;
import com.gxa.logistics.domain.entity.RoleMenu;
import com.gxa.logistics.domain.vo.RoleVO;
import com.gxa.logistics.mapper.MenuMapper;
import com.gxa.logistics.mapper.RoleMapper;
import com.gxa.logistics.mapper.RoleMenuMapper;
import com.gxa.logistics.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private MenuMapper menuMapper;


    @Override
    public RespResult<PageWrapperDTO<RoleVO>> list(RoleReqListDTO roleReqListDTO) {
        // 使用分页插件
        PageHelper.startPage(roleReqListDTO.getPageNo(), roleReqListDTO.getPageSize());
        // 包装的分页返回对象
        PageWrapperDTO<RoleVO> pageCommon = new PageWrapperDTO<>();
        List<RoleVO> roleList = roleMapper.findRoleByWhere(roleReqListDTO);
        // 设置分页返回对象
        PageResultUtil.setPageResult(roleList,pageCommon);
        return RespResult.success(pageCommon);
    }

    @Override
    public void insertRole(RoleSaveDTO roleSaveDTO) {
        roleMapper.insertRole(roleSaveDTO);
    }

    @Override
    public void updateRole(RoleUpdateDTO roleUpdateDTO) {
        roleMapper.updateRole(roleUpdateDTO);
    }

    @Override
    public void deleteRoleById(Integer roleId) {
        roleMapper.deleteRoleById(roleId);
    }

    @Override
    public void authorizeRole(RoleMenuSaveDTO roleMenuSaveDTO) {
        Integer roleId = roleMenuSaveDTO.getRoleId();
        List<Integer> menuIds = roleMenuSaveDTO.getMenuIds();
        //清除原来为角色分配的菜单
        roleMenuMapper.deleteByRoleId(roleId);

        for (Integer menuId : menuIds) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setMenuId(menuId);
            roleMenu.setRoleId(roleId);
            roleMenuMapper.insertRoleMenu(roleMenu);
        }

    }

    @Override
    public List<Menu> getRoleMenus(String flag) {
        Integer roleId = roleMapper.selectRoleIdByFlag(flag);

        List<Integer> menuIds = roleMenuMapper.selectMenuIdByRoleId(roleId);

        List<Menu> menus = menuMapper.findMenu();

        List<Menu> roleMenus = new ArrayList<>();

        for(Menu menu : menus){
            if(menuIds.contains(menu.getMenuId())){
                roleMenus.add(menu);
            }
            List<Menu> children = menu.getChildMenu();

            //移除children中menuId不在menuIds里的子菜单
            children.removeIf(child -> !menuIds.contains(child.getMenuId()));
        }
        return roleMenus;
    }

    @Override
    public List<RoleVO> findAll() {
        List<RoleVO> roles = roleMapper.findAll();
        return roles;
    }
}
